<?php

namespace app\admin\controller;

\think\Loader::import('controller/Controller', \think\Config::get('traits_path'), EXT);

use app\admin\Controller;
use think\Db;
use think\Config;

class DriverRankApply extends Controller
{
    use \app\admin\traits\controller\Controller;
    // 方法黑名单
    protected static $blacklist = [];

    /**
     * 首页
     * @return mixed
     */
    public function index()
    {
        if($this::$isdelete){
            $map['dra.isdelete']=1;
        }
        $data=input();

        $listRows = $this->request->param('numPerPage') ? : config("paginate.list_rows");
        $list = DB('DriverRankApply')
            ->alias('dra')
            ->join('tp_admin_user u','u.id=dra.user_id','left')
            ->join('tp_driver_rank dr','dr.id=dra.rank_id','left')
            ->field('dra.*,u.realname,u.mobile,dr.name as rank_name')
            ->where($map)
            ->order("dra.id desc")
            ->paginate($listRows, false, ['query' => $this->request->get()]);

        $this->view->assign('list',$list);
        $this->view->assign("count", $list->total());
        $this->view->assign("page", $list->render());
        $this->view->assign('numPerPage', $list->listRows());
        return $this->view->fetch();
    }

    /**
     * 通过并升级
     * @return mixed
     */
    public function agree()
    {
        //获取传递过来的数据
        $data = input();

        //根据传过来的id找到相应记录
        $driver_rank_apply = Db::name('driver_rank_apply')->where('id', $data['id'])->find();
        //查询司机等级,为0是新手
        $driver_rank_id = Db::name('driver')->where('user_id',$driver_rank_apply['user_id'])->value('driver_rank_id');
        if ($driver_rank_id > $driver_rank_apply['rank_id']){ //降级
            //查询降到该级的条件
            switch ($driver_rank_apply['rank_id']){
                case 0:
                    //去掉观看培训记录
                    $superiorIds = Db::name('driver_rank')->where('id','>',$driver_rank_apply['rank_id'])->field('id')->select();
                    $superior_id = '';
                    if ($superiorIds){
                        foreach ($superiorIds as $sik=>$siv){
                            $superior_id .= $siv['id'].',';
                        }
                        $superior_id = rtrim($superior_id,',');
                    }
                    $train = Db::name('training_materials')->where('rank_id','in',$superior_id)->field('id,title')->select();
                    foreach ($train as $tk=>$tv){
                        Db::name('training_materials_view')->where('user_id',$driver_rank_apply['user_id'])
                            ->where('training_materials_id',$tv['id'])->delete();
                    }
                    //去掉考试记录
                    $userPaperIds = Db::name('user_paper')->where('rank_id','in',$superior_id)
                        ->where('user_id',$driver_rank_apply['user_id'])
                        ->field('id')->select();
                    if ($userPaperIds){
                        foreach ($userPaperIds as $upik=>$upiv){
                            Db::name('user_paper_querstions')->where('user_paper_id',$upiv['id'])->where('user_id',$driver_rank_apply['user_id'])->delete();
                        }
                    }
                    break;//新手
                default:
                    $demotion = Db::name('driver_rank')->where('id',$driver_rank_apply['rank_id'])->find(); //降级数据
                    $driver_rank = $demotion['name'];
                    //去掉观看培训记录
                    $superiorIds = Db::name('driver_rank')->where('id','>',$driver_rank_apply['rank_id'])->field('id')->select();
                    $superior_id = '';
                    if ($superiorIds){
                    foreach ($superiorIds as $sik=>$siv){
                        $superior_id .= $siv['id'].',';
                    }
                    $superior_id = rtrim($superior_id,',');
                }
                    $train = Db::name('training_materials')->where('rank_id','in',$superior_id)->field('id,title')->select();
                    foreach ($train as $tk=>$tv){
                        Db::name('training_materials_view')->where('user_id',$driver_rank_apply['user_id'])
                                    ->where('training_materials_id',$tv['id'])->delete();
                    }
                    //去掉考试记录
                    $userPaperIds = Db::name('user_paper')->where('rank_id','in',$superior_id)
                                                ->where('user_id',$driver_rank_apply['user_id'])
                                                ->field('id')->select();
                    if ($userPaperIds){
                        foreach ($userPaperIds as $upik=>$upiv){
                            Db::name('user_paper')->where('id',$upiv['id'])->where('user_id',$driver_rank_apply['user_id'])->delete();
                            Db::name('user_paper_querstions')->where('user_paper_id',$upiv['id'])->where('user_id',$driver_rank_apply['user_id'])->delete();
                        }
                    }

            }
            set_info($driver_rank_apply['user_id'],"司机降级通知","你的司机等级被降为{$driver_rank}",'driver_rank_apply');
        }else{ //升级
            //查询该司机的订单量
            $demotion = Db::name('driver_rank')->where('id',$driver_rank_apply['rank_id'])->find();//到该等级的条件
            $driverData = Db::name('driver')->where('user_id',$driver_rank_apply['user_id'])->find();
            if ($demotion['order_num'] > $driverData['order_num']){
                // 更新司机的订单量
//                $update = Db::name('driver')->where('user_id',$driver_rank_apply['user_id'])->update(['order_num'=>$demotion['order_num']]);
            }
            set_info($driver_rank_apply['user_id'],"升级申请结果",'申请通过','driver_rank_apply');
        }
        //只要点过审核，就说明处理过
        $data['check'] = 1;
        $data['isagree'] = 1;
        //更新司机的等级
        Db::name('driver')->where('user_id',$driver_rank_apply['user_id'])->update(array('driver_rank_id'=>$driver_rank_apply['rank_id']));
        Db::name("driver_rank_apply")->where('id', $data['id'])->update($data);
        return ajax_return_adv('申请通过');

    }

    /**
     * 不通过
     * @return mixed
     */
    public function reject()
    {
        //获取传递过来的数据
        $data = input();

        //根据传过来的id找到相应记录
        $driver_rank_apply = Db::name('driver_rank_apply')->where('id', $data['id'])->find();

        //只要点过审核，就说明处理过
        $data['check'] = 1;

        $data['isagree'] = 0;
        set_info($driver_rank_apply['user_id'],"升级申请结果",'申请不通过','driver_rank_apply');

        Db::name("driver_rank_apply")->where('id', $data['id'])->update($data);
        return ajax_return_adv('申请不通过');

    }


    /**
     * 添加
     * @return mixed
     */
    public function add()
    {


        if ($this->request->isAjax()) {
            // 插入
            $data = $this->request->except(['id']);
            $data['create_time']=NOW_TIME;
            $ret = Db::name("driver_rank_apply")->insert($data);

            return ajax_return_adv('添加成功');
        } else {
            $users=Db::name("admin_user")->select();
            $ranks=Db::name("driver_rank")->select();
            $this->view->assign("users",$users);
            $this->view->assign("ranks",$ranks);
            // 添加
            return $this->view->fetch(  'edit');
        }
    }

    /**
     * 编辑
     * @return mixed
     */
    public function edit()
    {
        $data = input();
        if ($this->request->isAjax()) {
            $ret = Db::name("driver_rank_apply")->where('id',$data['id'])->update($data);

            return ajax_return_adv('修改成功');
        } else {
            $apply=Db::name("driver_rank_apply")->where('id',$data['id'])->find();
            $users=Db::name("admin_user")->select();
            $ranks=Db::name("driver_rank")->select();
            $this->view->assign("users",$users);
            $this->view->assign("ranks",$ranks);
            $this->view->assign("vo",$apply);
            // 添加
            return $this->view->fetch();
        }
    }

    /**司机详细资料**/
    public function details()
    {
        $data = input();

        $vo = Db::name('driver')->where('user_id', $data['user_id'])->find();
        if (!$vo){
            return ajax_return_adv_error('不存在该司机信息');
        }
        $users = Db::name('admin_user')->where('id',$vo['user_id'])->find();
        if ($users['id']){
            $users['user_id'] = $users['id'];
            unset($users['id']);
        }
        if (!$users){
            return ajax_return_adv_error('不存在该司机的用户信息',1);
        }
        //查询司机等级
        $driverRank = Db::name('driver_rank')->where('id',$vo['driver_rank_id'])->find();
        if ($driverRank){
            $vo['driver_rank_id'] = $driverRank['name'];
        }else{
            $vo['driver_rank_id'] = '新手';
        }
        $this->view->assign('users', $users);
        $this->view->assign('vo', $vo);
        return $this->view->fetch('details');
    }

}
